includes("xmake/toolchains/x86.lua")

includes("xmake/x86/bootblock.lua")

target("xv9")
    set_kind("binary")
    set_toolchains("x86-tc")
    set_filename("xv9.elf")
   
    add_cflags(
        "-m32",
        "-O2",
        "-fno-builtin",
        "-fno-pic -static",
        "-fno-pie -no-pie",
        "-fno-omit-frame-pointer",
        "-fno-strict-aliasing",
        "-fno-stack-protector",
        "-Wall -MD -ggdb -Werror",{force = true}
        )

    add_asflags(
        "-m32",
        "-gdwarf-2 -Wa,-divide",{force = true}
        )

    add_ldflags(
        "-m elf_i386",
        "-T kernel/kernel.ld",{force = true}
        )
    
    -- kernel
    add_files(
        "arch/x86/entry.S",
        "kernel/main.c",
        "kernel/spinlock.c",
        "kernel/printk.c",
        "kernel/console.c",
        "kernel/kalloc.c"
        )

    -- bsp
    add_files(
        "bsp/i386-qemu/bsp_debug.c"
       )

    -- drivers
    add_files(
        "drivers/uart/8250/8250.c"
        )

    add_includedirs(
        "arch",
        "arch/x86",
        "bsp/i386-qemu",
        "drivers",
        "kernel/include"
        )

    after_build(function(target)
        -- 生成 qemu 磁盘镜像
        os.exec("dd if=/dev/zero of=xv9.img count=10000")
        os.exec("dd if=bootblock of=xv9.img conv=notrunc")
        os.exec("dd if=%s of=xv9.img seek=1 conv=notrunc", target:targetfile())
	
        -- 启动 qemu
        os.exec("qemu-system-i386 -serial mon:stdio -drive file=xv9.img,index=0,media=disk,format=raw -smp 2 -m 512")
    end)
    
    after_clean(function(target)
        os.exec("rm xv9.img")
    end)
target_end()